package club.twelveblog.tblog.controller;

import club.twelveblog.tblog.common.constants.HttpStatusConstants;
import club.twelveblog.tblog.common.dto.Result;
import club.twelveblog.tblog.entity.BlogInfo;
import club.twelveblog.tblog.service.BlogInfoService;
import club.twelveblog.tblog.utils.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/tblog/blogInfo")
public class BlogController {
    @Autowired
    private BlogInfoService blogInfoService;

    /**
     * 根据blogId管理博客文章
     * @param blogId
     * @return
     */
    @GetMapping("/viewBlog/{blogId}")
    public ResponseEntity<BlogInfo> viewArticle(@PathVariable(name = "blogId") Long blogId) {

        BlogInfo article = blogInfoService.viewArticle(blogId);
        return ResponseEntity.status(HttpStatus.OK).body(article);
    }

    /**
     * 更新博客文章
     * @param blogInfo
     * @return
     */
    @PatchMapping("/updateArticle")
    public Result updateArticle(BlogInfo blogInfo){
        blogInfoService.updateArticle(blogInfo);
        return ResultGenerator.getResultByHttpNoData(HttpStatusConstants.UPDATE_SUCCESS);
    }

    /**
     * 禁止、允许评论
     * @param blogInfo
     * @return
     */
    @PatchMapping("/banComment")
    public Result banComment(BlogInfo blogInfo){
        blogInfoService.banComment(blogInfo);
        return ResultGenerator.getResultByHttpNoData(HttpStatusConstants.UPDATE_SUCCESS);
    }

    /**
     * 删除文章
     * @param blogId
     * @return
     */
    @PatchMapping("/deleteArticle")
    public Result deleteArticle(@PathVariable("blogId") Long blogId){
        blogInfoService.deleteArticle(blogId);
        return ResultGenerator.getResultByHttpNoData(HttpStatusConstants.DELETE_SUCCESS);
    }

    /**
     * 查找未删除的文章
     * @return
     */
    @GetMapping("/queryBlogNoDel")
    public Result<Page<List<BlogInfo>>> queryBlogNoDel(){
        Page<List<BlogInfo>> lists = blogInfoService.queryBlogsNoDel();
        return ResultGenerator.getResultByHttp(HttpStatusConstants.OK,lists);
    }

    /**
     * 根据分类名称查找对应博客文章
     * @param categoryName
     * @return
     */
    @GetMapping("/{categoryName}")
    public Result<Page<List<BlogInfo>>> findBlogInfoByCategoryName(@PathVariable("categoryName") String categoryName){
        Page<List<BlogInfo>> blogInfoList=blogInfoService.findBlogInfoByCategoryName(categoryName);
        return ResultGenerator.getResultByHttp(HttpStatusConstants.OK,blogInfoList);
    }
   }